package com.janal.model.user;

import com.janal.model.Basedao.Basedao;
import com.janal.util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    @Override
    public User getLoginUser(Connection conn, String usercode) throws Exception {
        User resultuser = null;
        ResultSet rs = null;
        PreparedStatement pstm = null;

        if(conn != null){
            String sql = "select * from user where usercode = ?";
            Object[] params = {usercode};
            rs = Basedao.execute(conn,sql,params,rs,pstm);
            if(rs.next()){
                resultuser = new User();
                resultuser.setUsercode(rs.getString("usercode"));
                resultuser.setPassword(rs.getString("password"));
            }
            JdbcUtils.close(rs,pstm,null);
        }
        return resultuser;
    }

    @Override
    public boolean setRegisterUser(Connection conn, User user) throws Exception {
        PreparedStatement pstm = null;
        if(conn != null){
            String sql = "insert into user(usercode,password,nickname,user_face," +
                    "user_sex,user_email,user_able) values(?,?,?,?,?,?,?)";
            Object[] params = {user.getUsercode(),user.getPassword(),user.getNickname(),user.getUser_face(),
                    user.getUser_sex(),user.getUser_email(),user.getUser_able()};
            int updateRows = Basedao.execute(conn,sql,params,pstm);
            JdbcUtils.close(pstm,null);
            if(updateRows > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean modifyMessage(Connection conn, User user) throws Exception {
        PreparedStatement pstm = null;
        if(conn != null){
            String sql = "update user set nickname=?,user_face=?,user_sex=?," +
                    "user_email=? where usercode = ?";
            Object[] params = {user.getNickname(),user.getUser_face(),user.getUser_sex(),
                               user.getUser_email(),user.getUsercode()};
            int updateRows = Basedao.execute(conn,sql,params,pstm);
            JdbcUtils.close(pstm,null);
            if(updateRows > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean modifyPassword(Connection conn, User user) throws Exception {
        PreparedStatement pstm = null;
        if(conn != null){
            String sql = "update user set password=? where usercode=?";
            Object[] params = {user.getPassword(),user.getUsercode()};
            int updateRows = Basedao.execute(conn,sql,params,pstm);
            JdbcUtils.close(pstm,null);
            if(updateRows > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public User getUserMessage(Connection conn,String usercode) throws Exception {
        User user = null;
        ResultSet rs = null;
        PreparedStatement pstm = null;
        if(conn != null){
            String sql = "select * from user where usercode = ?";
            Object[] params = {usercode};
            rs = Basedao.execute(conn,sql,params,rs,pstm);
            if(rs.next()){
                user = new User();
                user.setNickname(rs.getString("nickname"));
                user.setUser_face(rs.getString("user_face"));
                user.setUser_sex(rs.getString("user_sex"));
                user.setUser_email(rs.getString("user_email"));
            }
            JdbcUtils.close(rs,pstm,null);
        }
        return user;
    }
}
